Component({
  properties: {
    location: {
      type: Object,
      value: { longitude: 116.404, latitude: 39.915 }
    },
    stores: {
      type: Array,
      value: []
    }
  },

  data: {
    markers: [],
    longitude: 0,
    latitude: 0,
    mapScale: 16,
    isMapExpanded: false,
    startY: 0,
    startHeight: 0,
    currentMapHeight: 0,
    scrollTop: 0,
    lastTapTime: 0,
    velocity: 0,
    lastMoveTime: 0,
    lastMoveY: 0,
    minHeight: 200,
    maxHeight: 0,
    defaultHeight: 0
  },

  lifetimes: {
    ready() {
      this.calculateHeights();
      this.setData({
        currentMapHeight: this.data.defaultHeight,
        longitude: this.properties.location.longitude,
        latitude: this.properties.location.latitude
      });
    }
  },

  methods: {
    calculateHeights() {
      const systemInfo = wx.getSystemInfoSync();
      const windowHeight = systemInfo.windowHeight;
      this.setData({
        maxHeight: windowHeight * 0.7,
        defaultHeight: windowHeight * 0.33,
        minHeight: 200
      });
    },

    // 地图触摸开始
    onMapTouchStart(e) {
      this.setData({
        startY: e.touches[0].pageY,
        startHeight: this.data.currentMapHeight,
        lastMoveTime: Date.now(),
        lastMoveY: e.touches[0].pageY,
        velocity: 0
      });
    },
    
    // 地图触摸移动
    onMapTouchMove(e) {
      const currentY = e.touches[0].pageY;
      const deltaY = currentY - this.data.startY;
      let newHeight = this.data.startHeight - deltaY;
      
      // 限制高度范围
      newHeight = Math.max(this.data.minHeight, Math.min(this.data.maxHeight, newHeight));
      
      // 计算速度
      const now = Date.now();
      const timeDiff = now - this.data.lastMoveTime;
      if (timeDiff > 0) {
        const distance = currentY - this.data.lastMoveY;
        this.setData({
          velocity: distance / timeDiff,
          lastMoveTime: now,
          lastMoveY: currentY
        });
      }
      
      this.setData({
        currentMapHeight: newHeight,
        isMapExpanded: newHeight > (this.data.defaultHeight + 50)
      });
    },
    
    // 地图触摸结束
    onMapTouchEnd() {
      // 根据速度和当前位置决定最终状态
      const threshold = 0.3;
      let targetHeight;
      
      if (Math.abs(this.data.velocity) > threshold) {
        // 快速滑动
        if (this.data.velocity > 0) {
          // 向下滑动
          targetHeight = this.data.minHeight;
        } else {
          // 向上滑动
          targetHeight = this.data.maxHeight;
        }
      } else {
        // 慢速滑动，根据当前位置决定
        const midPoint = (this.data.maxHeight + this.data.minHeight) / 2;
        targetHeight = this.data.currentMapHeight > midPoint 
          ? this.data.maxHeight 
          : this.data.minHeight;
      }
      
      this.animateToHeight(targetHeight);
    },
    
    // 双击地图
    onMapDoubleTap(e) {
      const now = Date.now();
      if (now - this.data.lastTapTime < 300) {
        // 双击事件
        const targetHeight = this.data.isMapExpanded 
          ? this.data.minHeight 
          : this.data.maxHeight;
        this.animateToHeight(targetHeight);
      }
      this.setData({ lastTapTime: now });
    },
    
    // 动画到指定高度
    animateToHeight(targetHeight) {
      this.setData({
        currentMapHeight: targetHeight,
        isMapExpanded: targetHeight > (this.data.defaultHeight + 50),
        scrollTop: targetHeight === this.data.minHeight ? 0 : this.data.scrollTop
      });
      
      // 更新地图缩放控制
      if (targetHeight === this.data.maxHeight) {
        this.setData({ mapScale: 16 });
      }
    },
    
    // 地图放大
    onZoomIn() {
      this.setData({
        mapScale: Math.min(this.data.mapScale + 1, 20)
      });
    },
    
    // 地图缩小
    onZoomOut() {
      this.setData({
        mapScale: Math.max(this.data.mapScale - 1, 3)
      });
    },
    
    // 定位到当前位置
    onLocateMe() {
      this.setData({
        longitude: this.properties.location.longitude,
        latitude: this.properties.location.latitude,
        mapScale: 16
      });
    },
    
    // 点击门店
    onStoreClick(event) {
      const storeId = event.currentTarget.dataset.id;
      const currentCompany = this.properties.stores.find(item => item.id === storeId);
      if (currentCompany) {
        // 展开地图并定位
        this.animateToHeight(this.data.maxHeight);
        this.setData({
          longitude: currentCompany.longitude,
          latitude: currentCompany.latitude
        });
        
        this.triggerEvent('storeclick', { currentCompany });
      }
    },
    
    // 导航
    onNavigateTap(event) {
      event.stopPropagation();
      const storeId = event.currentTarget.dataset.id;
      const store = this.properties.stores.find(item => item.id === storeId);
      
      wx.openLocation({
        name: store.name,
        address: store.address,
        latitude: store.latitude,
        longitude: store.longitude,
      })
    },
    
    // 打电话
    onCallTap(event) {
      event.stopPropagation();
      const storeId = event.currentTarget.dataset.id;
      const store = this.properties.stores.find(item => item.id === storeId);
      
      if (store && store.phone) {
        wx.makePhoneCall({
          phoneNumber: store.phone
        });
      } else {
        wx.showToast({
          title: '该门店未提供电话',
          icon: 'none'
        });
      }
    },
    
    // 准备地图数据
    prepareMapData(location, stores) {
      const markers = stores.map(store => ({
        id: Number.parseInt(store.id),
        longitude: store.longitude,
        latitude: store.latitude,
        iconPath: '/images/ad.png',
        width: 30,
        height: 30,
        callout: {
          content: store.name,
          color: '#333',
          fontSize: 14,
          borderRadius: 4,
          bgColor: '#fff',
          padding: 5,
          display: 'ALWAYS'
        }
      }));
      
      this.setData({
        longitude: location.longitude,
        latitude: location.latitude,
        markers
      });
    }
  },

  observers: {
    'location, stores': function(location, stores) {
      if (location && stores && stores.length > 0) {
        this.prepareMapData(location, stores);
      }
    }
  }
});